/*
 * watermark 
 * 
 * process watermark for processImage
 * 
 * handle array of watermarks
 * so in the config file watermarks should
 * in [{"l":"a.png","position": "topLeft"}, {....}, ....] 
 * @l tell whether it for landscape or portait image
 * @position the position of watermark
 *
 */

var winston = require("winston");
var fs = require("fs");
var asyncGm = require("./asyncGm");// a wraper of gm make it thenable
var path = require("path");
var mysql = require("mysql");
var Promise = require("bluebird");

var watermark = function(){
	if(!(this instanceof watermark))
		return new watermark();
};
module.exports = watermark;

watermark.prototype.addOne = function(fileName){
	var self = this;
	var config = self.config;
	var current = self.watermarks[self.current];
	winston.info("current watermark: " + self.current);
	var newFileName = self.newFileName;
	var style = self.style;
	var configPath = self.configPath;
	var watermark = current[style] || "";
	var workFlow = this.workFlow;
	watermark = path.join(configPath, watermark);
	if(/(png|jpg|jpeg)$/i.test(watermark) && fs.existsSync(watermark)){
		asyncGm(watermark)
			.sizeAsync()
			.then(function(size){
				var aGm = this;
				var position = getPosition(size, self.fileSize, current.position);
				aGm.getNew(fileName)
					.quality(config.imageQuality)
					.command('composite')
					.in('-geometry', position)
					.in(watermark)
					.writeAsync(newFileName)
					.then(function(){
						winston.info('composite one watermark: ' + watermark + " to: " + newFileName);
						self.current++;
						winston.info("next watermark: " + self.current);
						if(self.current < self.length){
							self.addOne(newFileName);
							winston.info("new round");
						}else{
							workFlow.emit('processComplete');
						}
					})
					.catch(function(err){
						winston.error("#watermark @addOne writeAsync: " + err);
					});
			})
			.catch(function(err){
				winston.error("#watermark @addOne sizeAsync: " + err);
			});
		
	}else{
		winston.info("doesn't has " + style + " watermark");
		self.current++;
		winston.info("next watermark: " + self.current);
		if(self.current < self.length){
			self.addOne(fileName);
			winston.info("new round");
		}else{
			workFlow.emit('processComplete');
		}

	}
}

watermark.prototype.process = function(config, watermarks, fileName, style, size, configPath, workFlow, dbConfig, eventId, extNameRE){
	this.workFlow = workFlow;
	this.dbConfig = dbConfig;
	this.eventId = eventId;
	this.config = config;
	this.configPath = configPath;
	this.length = watermarks.length;
	this.watermarks = watermarks;
	this.fileName = fileName;
	this.newFileName = fileName.replace(extNameRE, "_c$1");
	this.fileSize = size;
	this.style = style;
	this.current = 0;
	this.addOne(fileName);
	winston.info("number of watermarks: " + this.length);
}


function getPosition(size, imgSize, position){
	var r;
	if(position == "topLeft"){
		r = "+0+0";
	}else if(position == "topMiddle"){
		r = "+" + Math.round((imgSize.width - size.width)/2) + "+0";
	}else if(position == "topRight"){
		r = "+" + Math.round(imgSize.width - size.width) + "+0";
	}else if(position == "middleLeft"){
		r = "+0+" + Math.round((imgSize.height - size.height)/2);
	}else if(position == "middle"){
		r = "+" + Math.round((imgSize.width - size.width)/2) + "+" + Math.round((imgSize.height - size.height)/2);
	}else if(position == "middleRight"){
		r = "+" + Math.round(imgSize.width - size.width) + "+" + Math.round((imgSize.height - size.height)/2);
	}else if(position == "bottomLeft"){
		r = "+0+" + Math.round(imgSize.height - size.height);
	}else if(position == "bottomMiddle"){
		r = "+" + Math.round((imgSize.width - size.width)/2) + "+" + Math.round(imgSize.height - size.height);
	}else if(position == "bottomRight"){
		r = "+" + Math.round(imgSize.width - size.width) + "+" + Math.round(imgSize.height - size.height);
	}else{
		r = position;
	}
	return r;
}
/*test getPosition
console.log(getPosition({width:80, height: 50}, {width:1152,height:768}, "topLeft"), "+0+0");
console.log(getPosition({width:80, height: 50}, {width:1152,height:768}, "topMiddle"), "+536+0");
console.log(getPosition({width:80, height: 50}, {width:1152,height:768}, "topRight"), "+1072+0");
console.log(getPosition({width:80, height: 50}, {width:1152,height:768}, "middleLeft"), "+0+359");
console.log(getPosition({width:80, height: 50}, {width:1152,height:768}, "middle"), "+536+359");
console.log(getPosition({width:80, height: 50}, {width:1152,height:768}, "middleRight"), "+1072+359");
console.log(getPosition({width:80, height: 50}, {width:1152,height:768}, "bottomLeft"), "+0+718");
console.log(getPosition({width:80, height: 50}, {width:1152,height:768}, "bottomMiddle"), "+536+718");
console.log(getPosition({width:80, height: 50}, {width:1152,height:768}, "bottomRight"), "+1072+718");
console.log(getPosition({width:80, height: 50}, {width:1152,height:768}, "+34+65"), "+34+65");
*/